import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/homepage',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/homepage',
                name: 'homepage',
                meta: {
                    title: '系统首页',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/homepage.vue'),
            },
            {
                path: '/editdevice',
                name: 'editdevice',
                meta: {
                    title: '编辑设备',
                    permiss: '3',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/editdevice.vue'),
            },
            {
                path: '/lightmodel',
                name: 'lightmodel',
                meta: {
                    title: '灯光模式',
                    permiss: '4',
                },
                component: () => import(/* webpackChunkName: "icon" */ '../views/lightmodel.vue'),
            },
            {
                path: '/edituser',
                name: 'edituser',
                meta: {
                    title: '用户管理',
                    permiss: '6',
                },
                component: () => import(/* webpackChunkName: "form" */ '../views/edituser.vue'),
            },
            {
                path: '/permission',
                name: 'permission',
                meta: {
                    title: '权限管理',
                    permiss: '7',
                },
                component: () => import(/* webpackChunkName: "permission" */ '../views/permission.vue'),
            },
            {
                path: '/systemmessage',
                name: 'systemmessage',
                meta: {
                    title: '系统消息',
                    permiss: '8',
                },
                component: () => import(/* webpackChunkName: "tabs" */ '../views/systemmessage.vue'),
            },
            {
                path: '/datavisualization',
                name: 'datavisualization',
                meta: {
                    title: '智慧大屏',
                    permiss: '9',
                },
                component: () => import(/* webpackChunkName: "charts" */ '../views/datavisualization.vue'),
            },
            {
                path: '/emergencymode',
                name: 'emergencymode',
                meta: {
                    title: '应急模式',
                    permiss: '10',
                },
                component: () => import(/* webpackChunkName: "donate" */ '../views/emergencymode.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心',
                },
                component: () => import(/* webpackChunkName: "user" */ '../views/user.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('ms_username');
    const permiss = usePermissStore();
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

export default router;
